/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Neil J. Patel <njpatel@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Neil J. Patel <njpatel@gmail.com>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomevfs/gnome-vfs.h>

#include "affinity.h"

//#include "aff-metabar.h"
#include "aff-results.h"
#include "aff-sidebar.h"
#include "aff-start.h"
#include "aff-window.h"
#include "tomboykeybinder.h"

static gboolean skip_taskbar = FALSE;
static gboolean systray = TRUE;

static GOptionEntry entries[] = 
{
  { "skip-taskbar", 's', 0, G_OPTION_ARG_NONE, &skip_taskbar, "Affinity window skips taskbar", NULL },
  { "disable-systray", 't', 0, G_OPTION_ARG_NONE, &systray, "Disable system tray icon", NULL },
  { NULL }
};

static void aff_make_status_menu (AffinityApp *app);
static void aff_status_icon_activated (GtkStatusIcon *icon, AffinityApp *app);

int
main (int argc, char **argv)
{
	AffinityApp *app;
	GnomeProgram *program;
		
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	GError *error = NULL;
	GOptionContext *context;

	context = g_option_context_new ("- Affinity Options");
	g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
	g_option_context_add_group (context, gtk_get_option_group (TRUE));
	g_option_context_parse (context, &argc, &argv, &error);	

	program = gnome_program_init (PACKAGE, VERSION,
				      LIBGNOME_MODULE , argc, argv,
				      GNOME_PARAM_GOPTION_CONTEXT, context,
				      GNOME_PARAM_HUMAN_READABLE_NAME, _("Affinity"),
				      GNOME_PARAM_APP_DATADIR, DATADIR,
				      NULL);	
	gtk_init (&argc, &argv);
	gnome_vfs_init ();

	gtk_set_locale ();
			      	
	if (skip_taskbar) {
		app = affinity_app_new (TRUE);
	} else {
		app = affinity_app_new (FALSE);
	}
	
	if (systray) {
		GtkStatusIcon *status;
		status = gtk_status_icon_new_from_icon_name ("search");
		app->status = status;
		gtk_status_icon_set_visible (status, TRUE);
		gtk_status_icon_set_tooltip (status, _("Affinity Search"));
		g_signal_connect (G_OBJECT (status), "activate",
				  G_CALLBACK (aff_status_icon_activated), (gpointer)app);
		aff_make_status_menu (app);
	}
	gtk_widget_show (app->window);	
	gtk_main();
}

/*******************************************************************************
				STATUS MENU
*******************************************************************************/

static void 
aff_status_icon_popup (GtkStatusIcon *icon, guint button,  guint activate_time, AffinityApp *app)
{
	gtk_menu_popup (GTK_MENU (app->status_menu), NULL,
                                                     NULL,
                                                     gtk_status_icon_position_menu,
                                                     (gpointer)app->status,
                                                     button,
                                                     activate_time);	
}

static void
aff_close (GtkMenuItem *menuitem, AffinityApp *app)
{
	
	//tomboy_keybinder_unbind (app->settings->key_binding, (TomboyBindkeyHandler)aff_global_modifier);
	affinity_app_close (app);
	gtk_main_quit();
}
static void
aff_make_status_menu (AffinityApp *app)
{
	GtkWidget *menu = gtk_menu_new ();
	app->status_menu = menu;
	GtkWidget *item;
	
	item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLOSE, NULL);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	
	g_signal_connect (G_OBJECT (item), "activate",
			 G_CALLBACK (aff_close), (gpointer)app);
	g_signal_connect (G_OBJECT (app->status), "popup-menu",
			  G_CALLBACK (aff_status_icon_popup), (gpointer)app);			 
	gtk_widget_show_all (menu);
	return;
}

static void 
aff_status_icon_activated (GtkStatusIcon *icon, AffinityApp *app)
{
	if (app->visible)
		affinity_app_hide (app);
	else {
		affinity_app_show (app);		
	}
}
